//
//  BookManager.m
//  03-NSArrayAndNSMutableArray
//
//  Created by 曹曹 on 16/7/6.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "BookManager.h"
#import "Book.h"

@implementation BookManager

+ (instancetype)manager {
    return [[BookManager alloc] init];
}
#pragma mark - 根据书名查找书籍
- (void)checkoutForBook:(NSString *)bookName {
    NSMutableArray *nameArray = [self getNameArray];
    if ([nameArray containsObject:bookName]) {
        NSInteger index = [nameArray indexOfObject:bookName];
        [self bookInformation:index];
    } else {
        NSLog(@"抱歉, 没有<<%@>>这本书!", bookName);
    }
}
#pragma mark - 输出单本书籍信息
- (void)bookInformation:(NSInteger)bookIndex {
    Book *book = [self.bookArray objectAtIndex:bookIndex];
    NSLog(@"书名<<%@>>, 售价:%@", book.bookName, book.bookPrice);
}
#pragma mark - 添加书籍
- (void)addBook:(NSString *)bookName price:(NSString *)price {
    NSMutableArray *nameArray = [self getNameArray];
    if ([nameArray containsObject:bookName]) {
        NSLog(@"已有<<%@>>这本书, 无需添加!", bookName);
    } else {
        Book *book = [Book bookWithName:bookName price:price];
        [self.bookArray addObject:book];        
    }
}
#pragma mark - 删除书籍
- (void)deleteBook:(NSString *)bookName {
    NSMutableArray *nameArray = [self getNameArray];
    if ([nameArray containsObject:bookName]) {
        NSInteger index = [nameArray indexOfObject:bookName];
        [self.bookArray removeObjectAtIndex:index];
    } else {
        NSLog(@"没有这本书, 无法完成删除操作!");
    }
}
#pragma mark - 显示所有书籍
- (void)showAllBooks {
    for (Book *book in self.bookArray) {
        NSLog(@"书名<<%@>>, 售价%@\n", book.bookName, book.bookPrice);
    }
}
#pragma mark - 根据书名修改价格
- (void)setBook:(NSString *)bookName price:(NSString *)price {
    NSMutableArray *nameArray = [self getNameArray];
    if ([nameArray containsObject:bookName]) {
        NSInteger index = [nameArray indexOfObject:bookName];
        Book *book = [self.bookArray objectAtIndex:index];
        book.bookPrice = price;
    } else {
        NSLog(@"没有这本书, 无法完成修改价格操作!");
    }
}
- (NSMutableArray *)getNameArray {
    NSMutableArray *nameArray = [NSMutableArray array];
    for (Book *book in self.bookArray) {
        [nameArray addObject:book.bookName];
    }
    return nameArray;
}
@end
